﻿using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace ManagedScenarios
{
    [Description("Throws an unhandled exception (crash)")]
    public class UnhandledExceptionScenario : IScenario
    {
        public void Run(string[] args)
        {
            // Throws the exception that triggers the dump
            var errorMessage = (args.Length > 0) ? args[0] : null;
            InstanceMethodThrowsException("Bad input", errorMessage);
        }

        [MethodImpl(MethodImplOptions.NoInlining)]
        private void InstanceMethodThrowsException(string input, string message)
        {
            StaticMethodThrowsException(input, message);
        }

        [MethodImpl(MethodImplOptions.NoInlining)]
        private static void StaticMethodThrowsException(string input, string message)
        {
            if (input.StartsWith("Bad", StringComparison.OrdinalIgnoreCase))
            {
                var actualMessage = message ?? "Intentional error";
                throw new InvalidOperationException(actualMessage);
            }
        }
    }
}
